package com.lcc.book.demos.web.Controller;

import com.lcc.book.demos.web.Mapper.BookInfoMapper;
import com.lcc.book.demos.web.Model.*;
import com.lcc.book.demos.web.Service.BookService;
import com.lcc.book.demos.web.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.util.HashMap;
import java.util.List;

@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired//注入依赖
    private BookService bookService;

//    private BookService bookService = new BookService();
//
//    public BookController() {
//        this.bookService = bookService;
//    }

    //图书列表展示
    @RequestMapping("/getBookListByPage")
    public Result getBookListByPage(PageRequest pageRequest, HttpSession session){
        log.info("查询翻页信息,pageReuest:{}" + pageRequest);
//        //用户登录校验
//        UserInfo userInfo = (UserInfo) session.getAttribute(Constants.SESSION_USER_KEY);
//        if(userInfo == null || userInfo.getId() <= 0 || "".equals(userInfo.getUsername())){
//            //校验失败
////            Result result = new Result();
////            result.setCode(ResultCode.UNLOGIN);
////            result.setErrMsg("用户未登录");
////            result.setData(null);
////            return result;
//            return Result.unlogin();
//        }
        //校验成功
        if (pageRequest == null || pageRequest.getOffset() < 0 || pageRequest.getPageSize() <= 0){
            log.warn("参数校验失败");
            return Result.fail("参数校验失败");
        }
        PageResult<BookInfo> bookInfos = null;
        try {
            bookInfos = bookService.selectBookInfoByPage(pageRequest);
            return Result.success(bookInfos);
        }catch (Exception e){
            return Result.fail(e.getMessage());
        }
        //以上成功、失败、未登录都需要重新实例化对象，然后进行结果对象属性的设置，
        // 对于其他增删改查操作都需要如此，故将成功、失败、未登录的做法提取出来作为Result类的成员方法，直接供其他方法调用
    }

    /**
     * 添加图书
     * @param bookInfo
     * @return
     */
    //这里返回的响应是String类型的，前端不能对字符串进行处理，所以需要转换成json对象的形式，前端也可以自行转换，
    // 但是为了统一返回给前端的格式，尽量还是后端进行处理
    @RequestMapping(value = "/addBook",produces = "application/json")
    public String addBookInfo(BookInfo bookInfo){
        log.info("接收添加图书请求，bookInfo:{}",bookInfo);
        //参数校验
        if(bookInfo == null || !StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getCount() < 0
                || bookInfo.getPrice() == null){
            return "参数校验失败，请检查入参";
        }
        Integer result = bookService.addBook(bookInfo);
        if (result != 1){
            //打印日志，有利于定位问题
            log.error("添加图书出错：bookInfo:{}",bookInfo);
            return "新增书籍失败，请联系管理员";
        }
        return "";
    }

    /**
     * 根据图书Id查询图书
     * @param bookId
     * @return
     */
    @RequestMapping("/queryBookInfo")
    public BookInfo queryBookInfo(Integer bookId){
        log.info("根据id查询图书，bookId:"  + bookId);
        //参数校验
        if(bookId == null){
            log.error("参数校验失败，请重新入参");
        }
        //调用service
        BookInfo bookInfo = bookService.selectBookInfoById(bookId);
        if (bookInfo == null){
            log.error("图书不存在：bookInfo:{}",bookInfo);
        }
        return bookInfo;
    }

    /**
     * 修改图书信息
     * @param bookInfo
     * @return
     */
    @RequestMapping(value = "/updateBookInfo",produces = "application/json")
    public String updateBookInfo(BookInfo bookInfo){
        log.info("接收到更新图书的请求，bookInfo:",bookInfo);
        //调用service
        Integer result = bookService.updateBookInfo(bookInfo);
        if (result != 1){
            log.error("更新图书信息失败，请联系管理员");
            return "更新图书信息失败，请联系管理员";
        }
        return "";
    }
    @RequestMapping(value = "/batchDelete",produces = "application/json")
    public String batchDelete(@RequestParam List<Integer> ids){
        log.info("接收请求, 批量删除图书, 图书ID:{}",ids);
        Integer result = bookService.batchDelete(ids);
        if (result<=0){
            log.error("批量删除失败, ids:{}",ids);
            return "批量删除失败, 请联系管理员";
        }
        return "";
    }


}
